package com.neuedu.oa.web.servlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neuedu.oa.dto.UserContext;
import com.neuedu.oa.service.UserService;

@WebServlet("/public-api/user/login")
public class LoginServlet extends AbstractAjaxHttpServlet {

	@Override
	public Object handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		// 1 获取参数(清理和验证参数)
		String account = req.getParameter("account");

		String password = req.getParameter("password");
		String captchaInput = req.getParameter("captcha");
		Object captcha = req.getSession().getAttribute("captcha");
		if (captcha != null) {
			if (!captcha.toString().equalsIgnoreCase(captchaInput)) {
				//throw new OAException("验证码输入错误，请重试");
			}
		}
		String loginIp = req.getRemoteHost();
		// 2 调用业务方法(进行转换)
		
		UserService userService =this.getBean(UserService.class); 
		UserContext uc = userService.login(account, password, loginIp);

		// 3把当前登录用户上下文放入session中
		this.saveCurrentUser(uc, req);

		return uc;
	}

}
